package shia.dao;

import shia.entity.PexProgram;
import shia.util.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PexProgramDao {

    private static Connection connection;

    static {
        connection = ConnectionUtil.getConnection();
    }

    public List<PexProgram> findPexProgram(String programCode) {
        List<PexProgram> pexProgramList = new ArrayList<PexProgram>();


        String sql;
        if (programCode != null) {
            sql = "SELECT * FROM pex_ydjd_cis_program_mapping WHERE program_code = ?";
        } else {
            sql = "SELECT * FROM pex_ydjd_cis_program_mapping ORDER BY update_time DESC";
        }

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            if (programCode != null) {
                preparedStatement.setString(1, programCode);
            }

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                PexProgram pexProgram = new PexProgram();
                pexProgram.setProgramCode(resultSet.getString("program_code"));
                pexProgram.setCmbProgramCode(resultSet.getString("cmb_program_code"));

                pexProgramList.add(pexProgram);
            }

            resultSet.close();
            preparedStatement.close();
            //TODO:close connection
        } catch (SQLException e) {
            //TODO:
            e.printStackTrace();
        }

        return pexProgramList;
    }

    public void savePexProgram(PexProgram pexProgram) {
        if (pexProgram == null) {
            return;
        }

        String sql = "INSERT INTO pex_ydjd_cis_program_mapping(`program_code`,`cmb_program_code`) VALUES(?,?)" +
                " ON DUPLICATE KEY UPDATE cmb_program_code=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, pexProgram.getProgramCode());
            preparedStatement.setString(2, pexProgram.getCmbProgramCode());
            preparedStatement.setString(3, pexProgram.getCmbProgramCode());

            preparedStatement.executeUpdate();

            preparedStatement.close();
            //TODO:close connection
        } catch (SQLException e) {
            e.printStackTrace();
            //TODO:
        }
    }

    public void savePexProgramList(List<PexProgram> pexProgramList) {
        if (pexProgramList.size() < 1) {
            return;
        }

        String sql = "INSERT INTO pex_ydjd_cis_program_mapping(`program_code`, `cmb_program_code`) VALUES(?,?)" +
                " ON DUPLICATE KEY UPDATE cmb_program_code=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            for (PexProgram pexProgram : pexProgramList) {
                preparedStatement.setString(1, pexProgram.getProgramCode());
                preparedStatement.setString(2, pexProgram.getCmbProgramCode());
                preparedStatement.setString(3, pexProgram.getCmbProgramCode());

                preparedStatement.addBatch();
            }

            //preparedStatement.executeUpdate();
            preparedStatement.executeBatch();

            preparedStatement.close();
            //TODO:close connection
        } catch (SQLException e) {
            e.printStackTrace();
            //TODO:
        }
    }
}
